﻿#region Licence
// <copyright file="Helpers/FileExtensionBuilder.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System.Collections.Generic;
using System.Text;
using GalaSoft.MvvmLight.Ioc;
using Interfaces;
using Interfaces.Enums;
using Microsoft.Win32;

#endregion

namespace Helpers
{
    /// <summary>
    /// Gestion des extensions de fichier pour le helper <see cref="IoHelper"/>
    /// </summary>
    public static class FileExtensionBuilder
    {
        /// <summary>
        /// Correspondance des <see cref="Interfaces.Enums.FileExtensionEnum"/>
        /// </summary>
        private static readonly Dictionary<FileExtensionEnum, FileExtensionDetail> DictionnaryFileExtension =
            new Dictionary<FileExtensionEnum, FileExtensionDetail>()
            {
                {FileExtensionEnum.AllFile, new FileExtensionDetail{Extension = "*.*", FilterText = "FilterIO_AllFiles"}},
                {FileExtensionEnum.Bmp, new FileExtensionDetail{Extension = "*.bmp", FilterText = "FilterIO_BmpFiles"}},
                {FileExtensionEnum.Doc, new FileExtensionDetail{Extension = "*.doc", FilterText = "FilterIO_DocFiles"}},
                {FileExtensionEnum.Docx, new FileExtensionDetail{Extension = "*.docx", FilterText = "FilterIO_DocxFiles"}},
                {FileExtensionEnum.Eps, new FileExtensionDetail{Extension = "*.eps", FilterText = "FilterIO_EpsFiles"}},
                {FileExtensionEnum.Gif, new FileExtensionDetail{Extension = "*.gif", FilterText = "FilterIO_GifFiles"}},
                {FileExtensionEnum.Jpeg, new FileExtensionDetail{Extension = "*.jpeg;*.jpg;*.jpe", FilterText = "FilterIO_JpegFiles"}},
                {FileExtensionEnum.Pdf, new FileExtensionDetail{Extension = "*.pdf", FilterText = "FilterIO_PdfFiles"}},
                {FileExtensionEnum.Png, new FileExtensionDetail{Extension = "*.png", FilterText = "FilterIO_PngFiles"}},
                {FileExtensionEnum.Xls, new FileExtensionDetail{Extension = "*.xls", FilterText = "FilterIO_XlsFiles"}},
                {FileExtensionEnum.Xlsx, new FileExtensionDetail{Extension = "*.xlsx", FilterText = "FilterIO_XlsxFiles"}},
                {FileExtensionEnum.Dwg, new FileExtensionDetail{Extension = "*.dwg", FilterText = "FilterIO_Dwgfiles"}},
                {FileExtensionEnum.Dxf, new FileExtensionDetail{Extension = "*.dxf", FilterText = "FilterIO_DxfFiles"}},
            };

        ///<summary>
        /// Retourne les détails de l'extension voulue
        ///</summary>
        ///<param name="extensionEnum">extension demandée</param>
        ///<returns>détail de l'extension demandée</returns>
        public static FileExtensionDetail GetFileExtensionDetail(FileExtensionEnum extensionEnum)
        {
            return DictionnaryFileExtension[extensionEnum];
        }

        /// <summary>
        /// Permet de renvoyer le <see cref="string"/> de filtre de la classe <see cref="OpenFileDialog"/> ou <see cref="SaveFileDialog"/>
        /// </summary>
        /// <param name="extensionEnum">Type d'extension voulue sur le filtre</param>
        /// <returns>La phrase du filtre</returns>
        public static string GetFilter(FileExtensionEnum extensionEnum)
        {
            var ext = DictionnaryFileExtension[extensionEnum];
            return  SimpleIoc.Default.GetInstance<IServiceLocalize>().LocalizedFromKey(ext.FilterText) + " (" + ext.Extension + ")|" + ext.Extension;
        }

        /// <summary>
        /// Retourne l'extension de fichier spécifiée
        /// </summary>
        /// <param name="extensionEnum">*Type d'extension voulu</param>
        /// <returns>Extension demandée</returns>
        public static string GetExtension(FileExtensionEnum extensionEnum)
        {
            var ext = DictionnaryFileExtension[extensionEnum];
            return ext.Extension;
        }

        /// <summary>
        /// Permet de renvoyer le <see cref="string"/> du filtre avec plusieurs extensions pour la classe <see cref="OpenFileDialog"/> ou <see cref="SaveFileDialog"/>
        /// </summary>
        /// <param name="includealltypes">Indique si l'on souhaite l'option "(Tous les fichiers | *.*)"  dans le filtre</param>
        /// <param name="extensionEnum">Types d'extensions souhaitées</param>
        /// <returns>La phrase du filtre</returns>
        public static string GetFilters(bool includealltypes = false, params FileExtensionEnum[] extensionEnum)
        {
            var filter = new StringBuilder();
            if (includealltypes)
            {
                //On commence par générer une option permettant de visualiser tous les types demandés
                filter.Append(SimpleIoc.Default.GetInstance<IServiceLocalize>().LocalizedFromKey("Filter_AllTypeFilesInclude"));
                filter.Append("|");
                var extensions = string.Empty;
                foreach (var fileExtensionEnum in extensionEnum)
                {
                    extensions += GetExtension(fileExtensionEnum) + ";";
                }
                extensions = extensions.Substring(0, extensions.Length - 1) + "|";
                filter.Append(extensions);
            }

            foreach (var fileExtension in extensionEnum)
            {
                filter.Append(GetFilter(fileExtension));
                filter.Append("|");
            }
            return filter.ToString().Substring(0, filter.ToString().Length - 1);
        }
    }

    /// <summary>
    /// Structure des extensions
    /// </summary>
    public struct FileExtensionDetail
    {
        public string Extension { get; set; }
        public string FilterText { get; set; }
    }
}
